import Vue from 'vue'
import vueRouter from 'vue-router'
Vue.use(vueRouter)


const router = new vueRouter({
  routes: [
    {
      path: '/',
      name: 'index',
      component: ()=> import('../views/login/index'),
      children:[
        {
          path: '/',
          component: ()=> import('../views/login/login')
        },
        {
          path: '/login',
          name: 'login',
          component: ()=> import('../views/login/login')
        },
        {
          path: '/register',
          name: 'register',
          component: ()=> import('../views/login/register')
        }
      ]
    },
    {
      path: '/main',
      name: 'main',
      meta: {title: '首页'},
      redirect: '/main',
      component: ()=> import('../Layout/index'),
      children: [
        {
          path: '/',
          component: ()=> import('../views/ATable/index'),
          meta: {title: 'A表单'}
        },
        {
          path: '/dashboard',
          name: 'dashboard',
          component: ()=> import('../views/dashboard/index'),
          meta: {title: '首页'}
        }
        ,{
          path: '/ATable',
          name: 'ATable',
          component: ()=> import('../views/ATable/index'),
          meta: {title: 'A表单'}
        }
        ,{
          path: '/BTable',
          name: 'BTable',
          component: ()=> import('../views/BTable/index'),
          meta: {title: 'B表单'}
        }
      ]
    },
    {
      path: '*',
      name: 404,
      component: ()=> import('../views/404')
    }
  ],
  mode: 'history', // require service support    //加上可去除url上的#号，且解决了每次进入主页面都需要刷新的问题
  scrollBehavior: () => ({ y: 0 }),
})

router.beforeEach((to,from,next) => {
  let flag = localStorage.getItem("flag");
  if(flag !== null) {
    next();
  }
  else{
    if(to.path === '/' || to.path === '/login' || to.path === '/register') next();
    else {
      alert("请先登录")
      next({
        path: '/'
      })
    }
  }
})

export default router
